﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DiscoveryLogic.Analytics.BusinessLogic;
using DiscoveryLogic.Analytics.BusinessLogic.Dedups;
using DiscoveryLogic.Analytics.BusinessLogic.Links;
using DiscoveryLogic.Analytics.BusinessLogic.Networks;
using DiscoveryLogic.Analytics.BusinessLogic.Rules;
using DiscoveryLogic.Analytics.DataObjects;
using DiscoveryLogic.Analytics.DataObjects.Dedup;
using DiscoveryLogic.Analytics.DataObjects.Networks;
using DiscoveryLogic.Analytics.DataObjects.Rules;
using DiscoveryLogic.Analytics.DataObjects.Visuals;
using DiscoveryLogic.Common.Utilities;

namespace DiscoveryLogic.Analytics.Modules.Explorers
{
    /// <summary>
    /// Interaction logic for ExploreEntityRelation.xaml
    /// </summary>
    public partial class ExploreEntityRelation : UserControl
    {
        private EntityDataSet _SrcEntitySet;
        private RuleType _RuleType;
        Dictionary<string,EntityRelationRule> rulesByNames=new Dictionary<string, EntityRelationRule>();

        public ExploreEntityRelation(
            EntityDataSet entitySet, RuleType ruleType)
        {
            InitializeComponent();
            this._SrcEntitySet = entitySet;
            this._RuleType = ruleType;
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            this.txtSrcDataSetName.Text = this._SrcEntitySet.DataSetName;
            this.cboRules.Items.Clear();

            List<RuleBase> rules=new List<RuleBase>();
            List<RuleBase> linkRules = 
                RuleManager.GetRulesByType(this._RuleType, EntityIcons.LinkRuleIcon);
            if (linkRules != null && linkRules.Count > 0)
                rules.AddRange(linkRules);

            this.rulesByNames=new Dictionary<string, EntityRelationRule>();
            if(rules !=null && rules.Count>0)
            {
                foreach(RuleBase rule in rules)
                {
                    this.cboRules.Items.Add(rule.RuleName);
                    if (rule is EntityRelationRule)
                        this.rulesByNames.Add(rule.RuleName, rule as EntityRelationRule);
                }
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            if(this.Parent is Window)
            {
                ((Window)this.Parent).Close();
            }
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            if(this.cboRules.SelectedIndex>=0 && !string.IsNullOrEmpty(this.txtNetworkName.Text))
            {
                try
                {
                    EntityRelationRule rule = this.rulesByNames[this.cboRules.SelectedItem.ToString()];
                    EntityRelationWorker worker = new EntityRelationWorker(
                        rule, this._SrcEntitySet.DataSetName, this.txtNetworkName.Text);
                    Network network = worker.ReadRelatedData();
                    if(NetworkDedupSetting.Exists(network.NetworkName))
                    {
                        NetworkDedupSetting dedupSetting = NetworkDedupSetting.Read(network.NetworkName);
                        NetworkDedupWorker dedupWorker = new NetworkDedupWorker(network, dedupSetting);
                        network = dedupWorker.DedupNetwork();
                    }
                    network.Save();

                    MyData.ReadAllNetworks();

                    if (this.Parent is Window)
                    {
                        ((Window)this.Parent).Close();
                    }
                }
                catch(Exception ex)
                {
                    ExceptionHandler.HandlesException(ex);
                    MessageBox.Show(ex.Message);
                }
            }
        }
    }
}
